package com.movie.service.impl;

import com.movie.dto.TagDTO;
import com.movie.entity.Tag;
import com.movie.repository.TagRepository;
import com.movie.service.TagService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class TagServiceImpl implements TagService {
    
    private final TagRepository tagRepository;
    
    @Override
    public List<TagDTO> findAll() {
        log.info("获取所有标签");
        List<Tag> tags = tagRepository.findAll();
        return tags.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    @Override
    public List<TagDTO> findHotTags() {
        log.info("获取热门标签");
        // 这里可以根据使用次数或其他标准来定义热门标签
        List<Tag> tags = tagRepository.findAll();
        return tags.stream()
                .filter(tag -> tag.getMovieCount() != null && tag.getMovieCount() > 10)
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    @Override
    public TagDTO findById(Long id) {
        log.info("根据ID获取标签: {}", id);
        Tag tag = tagRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("标签不存在"));
        return convertToDTO(tag);
    }
    
    @Override
    public TagDTO create(TagDTO tagDTO) {
        log.info("创建标签: {}", tagDTO.getName());
        Tag tag = convertToEntity(tagDTO);
        Tag saved = tagRepository.save(tag);
        return convertToDTO(saved);
    }
    
    @Override
    public TagDTO update(Long id, TagDTO tagDTO) {
        log.info("更新标签: {}", id);
        Tag existingTag = tagRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("标签不存在"));
        
        BeanUtils.copyProperties(tagDTO, existingTag, "id", "createdAt");
        Tag updated = tagRepository.save(existingTag);
        return convertToDTO(updated);
    }
    
    @Override
    public void delete(Long id) {
        log.info("删除标签: {}", id);
        if (!tagRepository.existsById(id)) {
            throw new RuntimeException("标签不存在");
        }
        tagRepository.deleteById(id);
    }
    
    @Override
    public Map<String, Object> getTagStatistics() {
        log.info("获取标签统计");
        List<Tag> tags = tagRepository.findAll();
        
        long totalTags = tags.size();
        List<String> hotTags = tags.stream()
                .filter(tag -> tag.getMovieCount() != null && tag.getMovieCount() > 10)
                .map(Tag::getName)
                .collect(Collectors.toList());
        
        Map<String, Integer> usageStats = tags.stream()
                .collect(Collectors.toMap(
                    Tag::getName,
                    tag -> tag.getMovieCount() != null ? tag.getMovieCount() : 0
                ));
        
        return Map.of(
            "totalTags", totalTags,
            "hotTags", hotTags,
            "usageStats", usageStats
        );
    }
    
    private TagDTO convertToDTO(Tag tag) {
        TagDTO dto = new TagDTO();
        BeanUtils.copyProperties(tag, dto);
        return dto;
    }
    
    private Tag convertToEntity(TagDTO dto) {
        Tag tag = new Tag();
        BeanUtils.copyProperties(dto, tag);
        return tag;
    }
}